//
//  HNSH_NoticeDetailViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#define kConentWidth        kScreenWidth - 92

#import "HNSH_NoticeDetailViewController.h"

#import "KKImageBrowser.h"

@interface HNSH_NoticeDetailViewController () <UIScrollViewDelegate>
/** */
@property (nonatomic, strong) UIScrollView *scrollView;
/** */
@property (nonatomic, strong) UIImageView *logoView;
/** */
@property (nonatomic, strong) UILabel *titleLab;
/** */
@property (nonatomic, strong) UILabel *contentLab;
/** */
@property (nonatomic, strong) UILabel *timeLab;

/* imgV */
@property (nonatomic, strong) UIImageView *imgV;

/* addBtn */
@property (nonatomic, strong) UIButton *addBtn;

@end

@implementation HNSH_NoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"公告详情" showLeftItem:YES];
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.logoView];
    [self.scrollView addSubview:self.titleLab];
    [self.scrollView addSubview:self.contentLab];
    [self.scrollView addSubview:self.timeLab];
    
    [self.view addSubview:self.addBtn];
}

- (void)addAllLayout{
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, kBottomSafeHeight+62, 0));
    }];
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(24);
        make.size.mas_equalTo(CGSizeMake(48, 48));
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.logoView.mas_right).offset(12);
        make.top.mas_equalTo(self.logoView.mas_top);
        make.width.mas_equalTo(kConentWidth);
        make.height.mas_equalTo(22);
    }];
    [self.contentLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.logoView.mas_right).offset(12);
        make.width.mas_equalTo(kConentWidth);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(8);
    }];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.top.mas_equalTo(self.contentLab.mas_bottom).offset(14);
        make.height.mas_equalTo(11);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (CGRectGetMaxY(self.timeLab.frame) > kScreenHeight - kNavBarAndStatusBarHeight) {
            self.scrollView.contentSize = CGSizeMake(kScreenWidth, CGRectGetMaxY(self.timeLab.frame) + 20);
        }
    });
}

//MARK: Setter Methods
- (void)setModel:(HNSH_NoticeModel *)model{
    _model = model;

    self.titleLab.text = model.title;
    self.contentLab.text = model.content;
    self.timeLab.text = model.publish_time;
    
    CGFloat height = [model.content heightForFont:self.contentLab.font width:kConentWidth] + 54;
    if (model.content.length > 0 && model.image_urls.count > 0) {
        height = height + 12;
    }
    for (int i = 0; i < model.image_urls.count; i ++) {
        NSString *url = model.image_urls[i];
        CGSize size = [self getImageWidthAndHeightWithImageString:url];
        UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(76, height, kConentWidth, (kConentWidth * (size.height / size.width)))];
        [imgV sd_setImageWithURL:[NSURL URLWithString:url]];
        imgV.tag = i;
        [self.scrollView addSubview:imgV];
        height = height + (kConentWidth * (size.height / size.width)) + 12;
        
        imgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        [imgV addGestureRecognizer:tap];
        
        if (i == model.image_urls.count-1) {
            self.imgV = imgV;
        }
    }
    
}

- (void)tapGesture:(UITapGestureRecognizer *)tap {
    UIView *view = tap.view;
    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.model.image_urls.count; i ++) {
        NSString *imageString = self.model.image_urls[i];
        KKImageBrowserModel *model = [[KKImageBrowserModel alloc] initWithURL:[NSURL URLWithString:imageString] toView:kSTWindow];
        [images addObject:model];
    }
    KKImageBrowser *vc = [[KKImageBrowser alloc] init];
    vc.images = images;
    vc.index = view.tag;
    [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:vc animated:NO completion:nil];
}

//MARK: Getter Methods
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.delegate = self;
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _scrollView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _scrollView;
}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(30, kScreenHeight - 50 - 6 - kBottomSafeHeight, kScreenWidth - 60, 50);
        [_addBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_addBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _addBtn.backgroundColor = [UIColor appGreenColor];
        _addBtn.layer.cornerRadius = 25;
        _addBtn.layer.masksToBounds = YES;
        WeakSelf(weakSelf);
        [[_addBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
    return _addBtn;
}

- (UIImageView *)logoView {
    if (!_logoView) {
        _logoView = [UIImageView new];
        _logoView.image = [UIImage imageNamed:@"mine_notifit_icon"];
    }
    return _logoView;
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = [UIFont boldSystemFontOfSize:14];
        _titleLab.textColor = [UIColor appBlackLabelColor];
        _titleLab.text = @"官方通告";
    }
    return _titleLab;
}

- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [UILabel new];
        _contentLab.font = [UIFont systemFontOfSize:12];
        _contentLab.numberOfLines = 0;
        _contentLab.textColor = [UIColor appGrayLabelColor];
    }
    return _contentLab;
}

- (UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [UILabel new];
        _timeLab.font = [UIFont systemFontOfSize:10];
        _timeLab.textColor = [UIColor colorWithHexString:@"#C7C7CC"];
    }
    return _timeLab;
}

- (CGSize)getImageWidthAndHeightWithImageString:(NSString *)string {
    //图片切割尾部
    NSArray *ary1 = [string componentsSeparatedByString:@"_"];
    //获得图片宽x高
    NSArray *ary2 = [ary1[1] componentsSeparatedByString:@"."];
    //获得图片宽高
    NSArray *ary3 = [ary2[0] componentsSeparatedByString:@"x"];
    return CGSizeMake([ary3[0] doubleValue], [ary3[1] doubleValue]);
}

@end
